package org.gd.hellojavacore.swing;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class PswKeyListener implements KeyListener {

    private JPasswordField passwordField;
    private StringBuilder password;

    public PswKeyListener(JPasswordField passwordField, StringBuilder password) {
        this.passwordField = passwordField;
        this.password = password;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
            if(password.length() == 0) return;
            password.deleteCharAt(password.length() - 1);
            return;
        }
        password.append(e.getKeyChar());
        passwordField.setText(password.toString());
        System.out.println(password.toString());
    }
    @Override
    public void keyPressed(KeyEvent e) {
    }
    @Override
    public void keyReleased(KeyEvent e) {
    }
}
